home *** CD-ROM | disk | FTP | other *** search
- Path: news.ichange.com!newsmaster
- From: patrick_widener@mail.amsinc.com (Patrick Widener)
- Newsgroups: comp.lang.c++
- Subject: Re: Why is a temporary created here?
- Date: Wed, 07 Feb 1996 14:12:36 GMT
- Organization: American Management Systems, Inc.
- Message-ID: <4fac8v$eur@ias2.ichange.com>
- References: <47ts5e$21o@ams.amsinc.com> <47vvhh$9ce@gabi.gabi-soft.fr>
- Reply-To: patrick_widener@mail.amsinc.com
- NNTP-Posting-Host: 162.70.176.43
- X-Newsreader: Forte Agent .99d/32.182
-
- kanze@gabi-soft.fr (J. Kanze) wrote:
-
- >Patrick Widener (patrick_widener@mail.amsinc.com) wrote:
- >
- >|> I have this situation:
- >
- >|> class B
- >|> {
- >|> ...
- >|> };
- >
- >|> class DOne : public B
- >|> {
- >|> ...
- >|> };
- >
- >|> class DTwo : public B
- >|> {
- >|> public:
- >|> void alfa ();
- >
- >|> protected:
- >
- >|> void bravo( B* &);
- >|> };
- >
- >|> void DTwo::alfa
- >|> {
- >|> DOne *pDOne;
- >|> DTwo *pDTwo;
- >|>
- >|> bravo (pDOne); // These lines are flagged by the compiler as
- >|> bravo (pDTwo); // warnings because temporaries are used for
- >|> // the formal argument of DTwo::bravo()
- >|> }
- >
- >|> I can't figure out why my compiler (Borland bcc32 4.51) is complaining
- >|> about the lines indicated above. To me, it seems that the pointers in
- >|> DTwo::alfa(), being pointers to types derived from B, should be
- >|> implicitly converted to B pointers. Someone enlighten me, please?
- >
- >And they are being implicitly converted. That's exactly what the
- >compiler is warning you about. The result of a conversion is a
- >temporary (a non-lvalue).
- >
- >According to the current draft, binding a non-lvalue to a non-const
- >reference is illegal, since any changes made through the reference will
- >only modify the temporary. This change was introduced at a later stage
- >in the language evolution (about 1990, I think), and most compilers only
- >generate a warning, rather than an error, to avoid breaking existing
- >code. But it is still illegal.
- >
- >From the above, it is not clear what you really want to do. Perhaps
- >declaring the parameter to bravo const would be enough, although in this
- >case, since you are dealing with pointers, just pass be value would have
- >the same effect. Alternatively, you could declare pDOne and pDTwo as
- >B*. Or create two bravo functions, one for each type. Or use an
- >explicit temporary of the correct type, and then assign it back to pDOne
- >or pDTwo after bravo returns.
- >--
- >James Kanze (+33) 88 14 49 00 email: kanze@gabi-soft.fr
- >GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
- >Conseils, Θtudes et rΘalisations en logiciel orientΘ objet --
- > -- A la recherche d'une activitΘ dans une region francophone
-
- Hmmm. OK, this sounds right. I wasn't as clear as I could have been
- in the original statement of the problem. In particular, the bravo()
- method *needs* an lvalue, since what it is doing in real life is
- pulling a specific instance of a class derived from B out of a
- dictionary maintained by DTwo. That's why I wanted to pass the
- pointer by reference, since I'm going to change it (assign to it) in
- bravo().
-
- I thought this was going to work because of the polymorphic behavior
- of the pointers, but I seem to remember something about parameter
- passing being modeled on initialization, not assignment, which I think
- would cause problems. oh well.
-
- thanks for responding...
-
- --------
- patrick widener
- patrick_widener@mail.amsinc.com http://pwidener.amsinc.com:2112
- "Ted Striker has more guts in his big toe than most of us have in
- our entire large intestine - INCLUDING the colon!!"
-